<?php

namespace App\Http\Requests;

use App\Common\Status;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

class NoteRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     * @return array
     */
    public function rules()
    {
        return [
            'id'                                     => 'nullable',
            'user_name'                              => 'required|string|max:30',
            'head_img'                               => 'nullable|integer',
            'sex'                                    => 'required',
            'annex_id'                                    => 'nullable|integer',
            'att_name'                                    => 'nullable|string',
            //            'education'                              => 'required|integer',
            'work_st'                                => 'required',
            'email'                                  => 'nullable|email',
            'mobile'                                 => 'required|regex:/^1[3456789]\d{9}$/',
            'birth'                                  => 'required',
            'work_date'                              => 'required',
            'weChat'                                 => 'nullable|string',
            'adv'                                    => 'nullable|string|max:800',
            'edu_id'                            => 'nullable|integer',
            'educational_back'                       => 'nullable|array',
            'educational_back.*.university_name'     => 'nullable|string',
            'educational_back.*.education'           => 'nullable|integer',
            'educational_back.*.st_date_st'          => 'nullable',
            'educational_back.*.st_date_ed'          => 'nullable',
            'educational_back.*.speciality'          => 'nullable|string',
            //            'educational_back.*.st_exp'              => 'required|string',
            'work_exp'                               => 'nullable|array',
            'work_exp.*.business_name'               => 'nullable|string',
            'work_exp.*.position_name'               => 'nullable|string',
            'work_exp.*.work_date_st'                => 'nullable',
            'work_exp.*.work_date_ed'                => 'nullable',
            'work_exp.*.salary'                      => 'nullable|numeric',
            'work_exp.*.work_exp'                    => 'nullable|string',
            'project_exp'                            => 'nullable|array',
            'project_exp.*.business_name'            => 'nullable|string',
            'project_exp.*.position_name'            => 'nullable|string',
            'project_exp.*.work_name'                => 'nullable|string',
            'project_exp.*.work_date_st'             => 'nullable',
            'project_exp.*.work_date_ed'             => 'nullable',
            'project_exp.*.work_exp'                 => 'nullable|string',
            'notes_intention'                        => 'nullable|array',
            'notes_intention.*.position_category_id' => 'nullable|integer',
            'notes_intention.*.job_type'             => 'nullable|integer',
            'notes_intention.*.salary1'              => 'nullable|numeric',
            'notes_intention.*.salary2'              => 'nullable|numeric',
            'notes_intention.*.work_province'        => 'nullable|integer',
            'notes_intention.*.work_city'            => 'nullable|integer'
        ];
    }


    public function messages(): array
    {
        return [
            'user_name.required'                              => '用户名称必须',
            //            'education.required'                              => '学历必须',
            'work_st.required'                                => '工作状态必须',
            'email.email'                                     => '邮箱有误',
            'mobile.regex'                                    => '手机格式有误',
            'birth.integer'                                   => '生日必须整形',
            'work_date.required'                              => '工作时间必须',
            'adv.max'                                         => '个人优势描述字符不超过200字符',
            'educational_back.array'                          => '教育背景',
            'educational_back.*.university_name.nullable'     => '毕业大学',
            'educational_back.*.education.nullable'           => '学历必须',
            'educational_back.*.st_date_st.nullable'          => '入学时间',
            'educational_back.*.st_date_ed.nullable'          => '毕业时间',
            'educational_back.*.speciality.nullable'          => '专业',
            //            'educational_back.*.st_exp.required'              => '在校必须',
            'work_exp.array'                                  => '企业经历',
            'work_exp.*.business_name.nullable'               => '公司名称',
            'work_exp.*.position_name.nullable'               => '职位',
            'work_exp.*.salary.nullable'                      => '薪资',
            'work_exp.*.work_date_st.nullable'                => '工作开始时间',
            'work_exp.*.work_date_ed.nullable'                => '工作结束时间',
            'work_exp.*.work_exp.nullable'                    => '工作经历',
            'notes_intention.array'                           => '企业经历',
            'notes_intention.*.position_category_id.nullable' => '职位',
            'notes_intention.*.job_type.nullable'             => '招聘类型',
            'notes_intention.*.salary1.nullable'              => '期望薪资',
            'notes_intention.*.salary2.nullable'              => '期望薪资',
            'notes_intention.*.work_city.nullable'            => '意向城市',
            'notes_intention.*.work_province.nullable'        => '意向所在省份',
            //            'project_exp.array'                           => '项目经历',
            //            'project_exp.*.business_name.required'        => '项目名称',
            //            'project_exp.*.position_name.required'        => '职位',
            //            'project_exp.*.work_date_st.required'         => '工作开始时间',
            //            'project_exp.*.work_date_ed.required'         => '工作结束时间',
            //            'project_exp.*.work_exp.required'             => '工作经历',
        ];
    }

    protected function failedValidation(Validator $validator)
    {
        $error = $validator->errors()->all();
        throw new HttpResponseException($this->fail(Status::ERROR, $error));
    }

    protected function fail(int $code, array $errors)
    {
        return response()->json(
            [
                'code'   => $code,
                'msg' => $errors,
            ]
        );
    }
}

